package com.tanhua.admin.module.usermanager.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.admin.module.usermanager.pojo.UserFreeze;
import com.tanhua.admin.module.usermanager.service.impl.UserManagerServiceImpl;
import com.tanhua.admin.module.usermanager.vo.PageResult;
import com.tanhua.admin.module.usermanager.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("manage")
public class UserManagerController {
    @Autowired
    private UserManagerServiceImpl userManagerService;

    @GetMapping("users")
    public ResponseEntity<Object> getUserStatus(@RequestParam Map<String, String> map){

        PageResult pageResult = this.userManagerService.getUserStatus(map);
        if (ObjectUtil.isEmpty(pageResult)){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/users/{userID}")
    public ResponseEntity<Object> getUserInfo(@PathVariable("userID") Long userId){

        UserInfoVo userInfoVo = userManagerService.getUserInfo(userId);
        if (ObjectUtil.isEmpty(userInfoVo)){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 冻结用户操作
     */
    @PostMapping("users/freeze")
    public ResponseEntity<Object> getUserFreeze(@RequestBody UserFreeze userFreeze){
        try {
            Integer message = userManagerService.getUserFreeze(userFreeze);
            if (1 == message){
                return ResponseEntity.ok("操作成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解冻用户操作
     */
    @PostMapping("users/unfreeze")
    public ResponseEntity<Object> getUserUnFreeze(@RequestBody Map<String,Object> map){
        try {
            Integer message = userManagerService.getUserUnFreeze(map);
            if (1 == message){
                return ResponseEntity.ok("操作成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
